package com.atguigu.async;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class FutureDemo02 {
    public static void main(String[] args) throws Exception {
        runAsync();
    }
    //发起一个异步请求
    public static void runAsync() {
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("你好runAsync");
                int a=10/0;
            }
        }).whenComplete(new BiConsumer<Void, Throwable>() {
            //类似于vue里面发起异步请求之后的then方法
            @Override
            public void accept(Void acceptVal, Throwable throwable) {
                System.out.println("获取上面执行之后的返回值"+acceptVal);
                System.out.println("whenComplete接受到上面发生的异常"+throwable);
            }
        }).exceptionally(new Function<Throwable, Void>() {
            @Override
            public Void apply(Throwable throwable) {
                System.out.println("exceptionally接受到上面发生的异常"+throwable);
                return null;
            }
        });
    }


}
